package ntut.andengine.physics;

import javax.microedition.khronos.opengles.GL10;

import ntut.andengine.game.GameLevel;

import org.anddev.andengine.entity.particle.ParticleSystem;
import org.anddev.andengine.entity.particle.emitter.PointParticleEmitter;
import org.anddev.andengine.entity.particle.initializer.VelocityInitializer;
import org.anddev.andengine.entity.particle.modifier.ColorModifier;
import org.anddev.andengine.entity.particle.modifier.ExpireModifier;
import org.anddev.andengine.entity.sprite.Sprite;
import org.anddev.andengine.extension.physics.box2d.PhysicsConnector;
import org.anddev.andengine.extension.physics.box2d.PhysicsFactory;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.bitmap.BitmapTexture;
import org.anddev.andengine.opengl.texture.bitmap.BitmapTextureRegionFactory;
import org.anddev.andengine.opengl.texture.region.TextureRegion;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.FixtureDef;

public class DeathLight extends PhysicalComponent {

	public enum Direction{
		Up,
		Down,
		Left,
		Right
	}
	
	PointParticleEmitter particleEmitter;
	ParticleSystem particleSystem;
	
	public DeathLight(GameLevel level, Direction direction) {
		super(level);
		
		this.mBitmapTexture = new BitmapTexture(64, 64, TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		TextureRegion lightTextureRegion = BitmapTextureRegionFactory.createFromAsset(this.mBitmapTexture, level, "20x20.png", 0, 0);
		TextureRegion laserTextureRegion = BitmapTextureRegionFactory.createFromAsset(this.mBitmapTexture, level, "laser.png", 0, 20);
		level.getEngine().getTextureManager().loadTexture(this.mBitmapTexture);			
		mSprite = new Sprite(0, 0, lightTextureRegion);
		mSprite.setColor(0, 0, 0, 0);
		mSprite.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE);
		
		FixtureDef fixtureDef = PhysicsFactory.createFixtureDef(1, 0, 0);
		fixtureDef.isSensor = true;
		mBody = PhysicsFactory.createBoxBody(level.getPhysicsWorld(), mSprite, BodyType.StaticBody, fixtureDef);
		mBody.setUserData(new ContactEvent("DeathLight"){

			@Override
			public void beginContact(Body body, Vector2 Normal) {
			
			}

			@Override
			public void endContact(Body body, Vector2 Normal) {
				
			}});
		
		particleEmitter = new PointParticleEmitter(0, 0);
		particleSystem = new ParticleSystem(particleEmitter, 1, 1, 10, laserTextureRegion);
		
		switch(direction){
		case Up:
			mSprite.setRotation(90);
			particleSystem.addParticleInitializer(new VelocityInitializer(0, 0, -20, -20));
		case Down:
			mSprite.setRotation((float) (Math.PI/2));
			particleSystem.addParticleInitializer(new VelocityInitializer(0, 0, 20, 20));
		case Left:
			mSprite.setRotation(180);
			particleSystem.addParticleInitializer(new VelocityInitializer(-20, -20, 0, 0));
		case Right:
			particleSystem.addParticleInitializer(new VelocityInitializer(20, 20, 0, 0));
		}
		
		particleSystem.addParticleModifier(new ColorModifier(1, 0, 1, 0, 0, 0, 0, 1));
		particleSystem.setBlendFunction(GL10.GL_SRC_ALPHA, GL10.GL_ONE);
		particleSystem.addParticleModifier(new ExpireModifier(1, 1));		
		
		level.getScene().attachChild(particleSystem);		
		level.getScene().attachChild(mSprite);
		level.getPhysicsWorld().registerPhysicsConnector(new PhysicsConnector(mSprite, mBody, true, true));
		
	}

	public PointParticleEmitter getParticleEmitter(){
		return particleEmitter;
	}
	
	public ParticleSystem getParticleSystem(){
		return particleSystem;
	}
	
	public void remove(){
		this.mBody.setActive(false);
		this.getParticleSystem().setParticlesSpawnEnabled(false);
	}
}
